<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高级弹幕系统 - 悬浮减速功能</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            color: #fff;
            min-height: 100vh;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
            width: 100%;
            max-width: 800px;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        #video-container {
            position: relative;
            width: 100%;
            max-width: 800px;
            height: 450px;
            margin: 0 auto 20px;
            background-color: #000;
            overflow: hidden;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
        }

        #container {
            position: relative;
            width: 100%;
            height: 100%;
        }

        .bullet {
            position: absolute;
            font-size: 20px;
            font-weight: bold;
            color: white;
            text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.7);
            white-space: nowrap;
            overflow: hidden;
            display: flex;
            align-items: center;
            padding: 8px 15px;
            border-radius: 25px;
            background-color: rgba(0, 0, 0, 0.6);
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
        }

        .bullet:hover {
            background-color: rgba(0, 0, 0, 0.8);
            transform: scale(1.05);
            z-index: 10;
        }

        .input-section {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            width: 90%;
            display: flex;
            background: rgba(0, 0, 0, 0.7);
            padding: 15px;
            border-radius: 50px;
            backdrop-filter: blur(5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .input-section input {
            flex: 1;
            padding: 12px 20px;
            font-size: 16px;
            border: none;
            border-radius: 30px 0 0 30px;
            outline: none;
            background: rgba(255, 255, 255, 0.9);
        }

        .input-section button {
            padding: 12px 25px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            background: linear-gradient(to right, #ff416c, #ff4b2b);
            color: white;
            border: none;
            border-radius: 0 30px 30px 0;
            transition: all 0.3s ease;
        }

        .input-section button:hover {
            background: linear-gradient(to right, #ff4b2b, #ff416c);
            transform: scale(1.03);
        }

        .delete-text {
            margin-left: 10px;
            padding: 5px 10px;
            font-size: 14px;
            cursor: pointer;
            background-color: rgba(255, 0, 0, 0.7);
            color: white;
            border-radius: 15px;
            display: none;
            transition: all 0.3s ease;
        }

        .bullet:hover .delete-text {
            display: block;
        }

        .delete-text:hover {
            background-color: rgba(255, 0, 0, 0.9);
            transform: scale(1.1);
        }

        .controls {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            width: 100%;
            max-width: 800px;
            background: rgba(0, 0, 0, 0.3);
            padding: 20px;
            border-radius: 15px;
            margin-bottom: 20px;
        }

        .control-group {
            flex: 1;
            min-width: 200px;
        }

        .control-group h3 {
            margin-bottom: 10px;
            font-size: 1.2rem;
        }

        .color-picker {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .color-option {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            cursor: pointer;
            border: 2px solid transparent;
            transition: all 0.2s ease;
        }

        .color-option:hover, .color-option.active {
            transform: scale(1.1);
            border: 2px solid white;
        }

        .slider-container {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .slider-container input {
            flex: 1;
        }

        .history {
            width: 100%;
            max-width: 800px;
            background: rgba(0, 0, 0, 0.3);
            padding: 20px;
            border-radius: 15px;
        }

        .history h2 {
            margin-bottom: 15px;
            text-align: center;
        }

        .history-list {
            max-height: 150px;
            overflow-y: auto;
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }

        .history-item {
            background: rgba(255, 255, 255, 0.15);
            padding: 8px 15px;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.2s ease;
        }

        .history-item:hover {
            background: rgba(255, 255, 255, 0.25);
            transform: translateY(-2px);
        }

        .footer {
            margin-top: 20px;
            text-align: center;
            font-size: 0.9rem;
            opacity: 0.8;
        }

        @media (max-width: 600px) {
            .input-section {
                flex-direction: column;
                border-radius: 20px;
            }

            .input-section input {
                border-radius: 20px 20px 0 0;
                margin-bottom: 10px;
            }

            .input-section button {
                border-radius: 0 0 20px 20px;
            }

            .header h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
<div class="header">
    <h1>✨ 弹幕系统 ✨</h1>
</div>

<div id="video-container">
    <div id="container"></div>
    <div class="input-section">
        <input type="text" id="messageInput" placeholder="输入弹幕内容...">
        <button id="sendButton">发送弹幕</button>
    </div>
</div>

<div class="controls">
    <div class="control-group">
        <h3>弹幕颜色</h3>
        <div class="color-picker" id="colorPicker">
            <div class="color-option active" style="background-color: #FF5252;" data-color="#FF5252"></div>
            <div class="color-option" style="background-color: #FFD740;" data-color="#FFD740"></div>
            <div class="color-option" style="background-color: #7C4DFF;" data-color="#7C4DFF"></div>
            <div class="color-option" style="background-color: #18FFFF;" data-color="#18FFFF"></div>
            <div class="color-option" style="background-color: #69F0AE;" data-color="#69F0AE"></div>
            <div class="color-option" style="background-color: #FFFFFF;" data-color="#FFFFFF"></div>
        </div>
    </div>

    <div class="control-group">
        <h3>弹幕速度</h3>
        <div class="slider-container">
            <input type="range" id="speedSlider" min="1" max="10" value="3">
            <span id="speedValue">3</span>
        </div>
    </div>

    <div class="control-group">
        <h3>减速效果</h3>
        <div class="slider-container">
            <input type="range" id="slowdownSlider" min="1" max="10" value="3">
            <span id="slowdownValue">3</span>
        </div>
    </div>
</div>

<div class="history">
    <h2>发送历史</h2>
    <div class="history-list" id="historyList"></div>
</div>

<div class="footer">
    <p>高级弹幕系统 | 悬浮减速功能 | 2023</p>
</div>

<script>
    // 获取DOM元素
    const container = document.getElementById('container');
    const messageInput = document.getElementById('messageInput');
    const sendButton = document.getElementById('sendButton');
    const colorPicker = document.getElementById('colorPicker');
    const speedSlider = document.getElementById('speedSlider');
    const speedValue = document.getElementById('speedValue');
    const slowdownSlider = document.getElementById('slowdownSlider');
    const slowdownValue = document.getElementById('slowdownValue');
    const historyList = document.getElementById('historyList');

    // 定义弹幕数据
    let data = [];
    let speed = 3;
    let slowdownFactor = 3; // 减速系数
    let interval = 800;
    let topArr = [];
    let index = 0;
    let selectedColor = '#FF5252';
    let history = [];

    // 初始化弹幕轨道
    for (let i = 0; i < 10; i++) {
        topArr.push(i * 40);
    }

    // 设置颜色选择器
    colorPicker.querySelectorAll('.color-option').forEach(option => {
        option.addEventListener('click', () => {
            colorPicker.querySelector('.active').classList.remove('active');
            option.classList.add('active');
            selectedColor = option.dataset.color;
        });
    });

    // 设置速度滑块
    speedSlider.addEventListener('input', () => {
        speed = parseInt(speedSlider.value);
        speedValue.textContent = speed;
    });

    // 设置减速滑块
    slowdownSlider.addEventListener('input', () => {
        slowdownFactor = parseInt(slowdownSlider.value);
        slowdownValue.textContent = slowdownFactor;
    });

    // 发送弹幕
    sendButton.addEventListener('click', sendDanmu);
    messageInput.addEventListener('keypress', function(event) {
        if (event.key === 'Enter') {
            sendDanmu();
        }
    });

    function sendDanmu() {
        const message = messageInput.value.trim();
        if (message) {
            data.push({ text: message, color: selectedColor });

            // 添加到历史记录
            addToHistory(message);

            messageInput.value = '';
            messageInput.focus();
        }
    }

    // 添加到历史记录
    function addToHistory(message) {
        history.push(message);
        if (history.length > 10) {
            history.shift();
        }
        updateHistoryList();
    }

    // 更新历史记录列表
    function updateHistoryList() {
        historyList.innerHTML = '';
        history.forEach((msg, index) => {
            const item = document.createElement('div');
            item.className = 'history-item';
            item.textContent = msg;
            item.addEventListener('click', () => {
                messageInput.value = msg;
                messageInput.focus();
            });
            historyList.appendChild(item);
        });
    }

    // 定时发送弹幕
    setInterval(function() {
        if (data.length > 0) {
            const bulletData = data[index];

            // 创建弹幕元素
            const bullet = document.createElement('div');
            bullet.className = 'bullet';
            bullet.style.color = 'white';
            bullet.style.backgroundColor = bulletData.color;
            bullet.textContent = bulletData.text;

            // 随机获取弹幕位置
            const top = topArr[Math.floor(Math.random() * topArr.length)];
            bullet.style.top = top + 'px';

            // 将弹幕元素添加到容器中
            container.appendChild(bullet);

            // 计算弹幕宽度
            const width = bullet.offsetWidth;

            // 定义弹幕起始位置
            let left = container.offsetWidth;
            bullet.style.left = left + 'px';

            // 定义弹幕移动定时器
            const bulletTimer = setInterval(function() {
                // 判断是否鼠标悬浮
                if (bullet.isHovered) {
                    left -= speed / slowdownFactor; // 应用减速系数
                } else {
                    left -= speed;
                }
                bullet.style.left = left + 'px';

                // 判断弹幕是否移出容器
                if (left < -width) {
                    // 移除弹幕元素
                    container.removeChild(bullet);
                    // 清除弹幕移动定时器
                    clearInterval(bulletTimer);
                }
            }, 10);

            // 保存定时器ID以便删除
            bullet.bulletTimer = bulletTimer;

            // 更新弹幕索引
            index = (index + 1) % data.length;

            // 为弹幕添加事件监听
            bullet.addEventListener('mouseenter', function() {
                this.isHovered = true;

                // 创建删除按钮
                if (!this.querySelector('.delete-text')) {
                    const deleteText = document.createElement('span');
                    deleteText.className = 'delete-text';
                    deleteText.textContent = '删除';
                    this.appendChild(deleteText);
                }
            });

            bullet.addEventListener('mouseleave', function() {
                this.isHovered = false;

                // 移除删除按钮
                const deleteText = this.querySelector('.delete-text');
                if (deleteText) {
                    this.removeChild(deleteText);
                }
            });
        }
    }, interval);

    // 删除弹幕
    container.addEventListener('click', function(event) {
        // 如果点击的是删除按钮
        if (event.target.classList.contains('delete-text')) {
            const bullet = event.target.parentElement;
            clearInterval(bullet.bulletTimer);
            container.removeChild(bullet);
        }
    });

    // 初始化一些示例弹幕
    setTimeout(() => {
        data.push({ text: '欢迎使用弹幕系统！', color: '#7C4DFF' });
        data.push({ text: '试试悬浮减速效果', color: '#FFD740' });
        data.push({ text: '鼠标悬停可减速查看', color: '#18FFFF' });
        data.push({ text: '点击删除按钮可移除弹幕', color: '#69F0AE' });
        data.push({ text: '可以调整弹幕速度和减速效果', color: '#FFFFFF' });
    }, 1000);
</script>
</body>
</html>